﻿#include "PageWidget.h"
#include "ui_PageWidget.h"
#include "components/FWidgets/FSingleLineEditDialog/FSingleLineEditDialog.h"

#include "components/FWidgets/FMessageBox/FMessageBox.h"

PageWidget::PageWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PageWidget)
{
    ui->setupUi(this);
    QList<int> sizes;
    sizes << width() * 0.15 << width() * 0.85;
    ui->splitter->setSizes(sizes);

    // 先清空界面：
    removePage(0);  // 这行代码不可移除
    removePage(0);  // 这行代码不可移除

    connect(ui->lwPages, &ClosableListWidget::itemCloseRequested, this, &PageWidget::onItemCloseRequested);
    connect(ui->lwPages, &ClosableListWidget::itemActivated, this, &PageWidget::onItemActived);
}

PageWidget::~PageWidget()
{
    delete ui;
}

void PageWidget::addPage(const QString &pageName, QWidget* page)
{
    ClosableListWidgetItem* newItem = ui->lwPages->addItem(pageName);
    setItemContextMenu(newItem);
    // page->setTitle(pageName);
    ui->swPages->addWidget(page);
}

void PageWidget::setPage(int pageID)
{
    if (pageID < 0) return;
    if (pageID < ui->lwPages->count()) {

        for (int i = 0; i < ui->lwPages->count(); i++) {
            ui->lwPages->item(i)->setIcon(QIcon());
        }

        ClosableListWidgetItem* item = ui->lwPages->item(pageID);
        // ui->pageTitle->setTitle(item->text());
        item->setIcon(QIcon(":/imgs/current.png"));
    }
    if (pageID < ui->swPages->count())
        ui->swPages->setCurrentIndex(pageID);
}

void PageWidget::removePage(int pageID)
{
    if (pageID < 0) return;
    if (pageID < ui->lwPages->count())
        ui->lwPages->closeItem(pageID);
    if (pageID < ui->swPages->count())
        ui->swPages->removeWidget(ui->swPages->widget(pageID));
    setPage(currentPageID());
}

int PageWidget::currentPageID() const
{
    return ui->swPages->currentIndex();
}

int PageWidget::pageCount() const
{
    return ui->swPages->count();
}

void PageWidget::setItemContextMenu(ClosableListWidgetItem* item)
{
    QMenu* menu = item->contextMenu();
    QAction* actRename = new QAction(tr("Rename"), menu);
    connect(actRename, &QAction::triggered, this, [=](){
        QString name = FSingleLineEditDialog::getString(this, tr("Input"), tr("Rename page name below:"), item->text());
        if (!name.isEmpty()) {
            item->setText(name);
            emit pageNameChanged(ui->lwPages->currentRow(), name);
            if (currentPageID() == ui->lwPages->currentRow()) {
                // ui->pageTitle->setTitle(name);
                // panelAt(currentPageID())->setTitle(name);
            }
        }
    });
    menu->addAction(actRename);
}

QWidget *PageWidget::panelAt(int pageID)
{
    if (pageID < 0 || pageID >= ui->swPages->count()) return nullptr;
    return ui->swPages->widget(pageID);
}

void PageWidget::onItemCloseRequested(int row)
{
    auto res = FMessageBox::question(this, tr("Question"), tr("Sure to remove this page?"));
    if (res == FMessageBox::StandardButton::Yes) {
        removePage(row);
    }
}

void PageWidget::onItemActived(ClosableListWidgetItem* item)
{
    int row = ui->lwPages->row(item);
    setPage(row);
}

void PageWidget::on_btnAddPage_clicked()
{
    onAddClicked();
    setPage(currentPageID());
}
